<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Logics\ChannelLogic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Logics\CategoryLogic;
use Illuminate\Support\Facades\Storage;

class CategoryController extends BaseController{

    private function getInstance(){
        static $ob=null;
        if(is_null($ob)){
            $ob= new CategoryLogic();
        }
        return $ob;
    }
    //分类列表
    public function index()
    {
        $cha_id = (int)request('cha_id', 0);
        $para=[
            'cha_id'=>$cha_id
        ];
        $ob= $this->getInstance();
        $data=$ob->index($para);
        if($data && is_string($data)) die(json_error($data));

        $content= view("admin.category.categoryList",$data) ;
        $data=[
           'content'=>$content,
           'header'=>'分类列表',
        ];
        return  $this->baseList($data);
    }



    //分类添加
    public function add(Request $request){
        if($_POST){
            $cha_id=(int)request('cha_id',0);
            $cate_name=trim(request('cate_name'));
            $cate_note=trim(request('cate_note'));
            $cate_code=trim(request('cate_code'));
            $cate_code_name=trim(request('cate_code_name'));
            $cate_hot_search=trim(request('cate_hot_search'));
            $pid=(int)request('pid',0);

            $para=[
                'cha_id'=>$cha_id,
                'cate_name'=>$cate_name,
                'cate_note'=>$cate_note,
                'cate_code'=>$cate_code,
                'cate_code_name'=>$cate_code_name,
                'cate_hot_search'=>$cate_hot_search,
                'pid'=>$pid
            ];
            $ob= $this->getInstance();
            $data=$ob->add($para,$request);
            $this->result($data);
        }

        $channel= ChannelLogic::pluck('cha_describe', 'cha_id',[['cha_status',2]]);
        $content= view("admin.category.categoryAdd",['channel'=>$channel]) ;

        $data=[
            'content'=>$content,
            'header'=>'分类添加分类',
        ];
        return  $this->baseList($data);
    }

    public function edit(Request $request){
        if($_POST){
            $cate_name=trim(request('cate_name'));
            $cate_note=trim(request('cate_note'));
            $cate_code=trim(request('cate_code'));
            $cate_code_name=trim(request('cate_code_name'));
            $cate_hot_search=trim(request('cate_hot_search'));
            $pid=(int)request('pid',0);
            $cate_id=(int)request('cate_id');
            $p_sort=(int)request('p_sort',10);
            $is_del=(int)request('is_del',0);
            $is_hidden=(int)request('is_hidden',2);

            $para=[
                'cate_name'=>$cate_name,
                'cate_note'=>$cate_note,
                'cate_code'=>$cate_code,
                'cate_code_name'=>$cate_code_name,
                'cate_hot_search'=>$cate_hot_search,
                'pid'=>$pid,
                'cate_id'=>$cate_id,
                'p_sort'=>$p_sort,
                'is_del'=>$is_del,
                'is_hidden'=>$is_hidden
            ];

            $ob= $this->getInstance();
            $data=$ob->edit($para,$request);
            $this->result($data);
        }


        $id=(int)request('id');
        $ob=$this->getInstance();
        $data=$ob->getEdit($id);
        if($data && is_string($data)) die(json_error($data));


        $content= view("admin.category.categoryEdit",$data) ;
        $data=[
            'content'=>$content,
            'header'=>'分类编辑',
        ];
        return  $this->baseList($data);
    }



    //获取分类根据渠道
    public function getCategory(){
        $cha_id=(int)request('cha_id');
        $ob= $this->getInstance();
        $data=$ob->getCategoryHtml($cha_id);
        $this->result($data);
    }

    //删除分类的图片
    public function delImg()
    {
       if($_POST){
           $cate_id=(int)request('cate_id');
           $ob= $this->getInstance();
           $data=$ob->delImg($cate_id);
           $this->result($data);
       }

        return json_error('失败');
    }


    /**
     * 获取子类html
     */
    public function getChildHtml(){
        $id=(int)request('id');

        $ob= $this->getInstance();
        $html=$ob->getChildHtml($id);


        if($html){
            die(json_success('ok',['html'=>$html]));
        }
        die(json_error('获取失败'));
    }


}